package tw.com.softleader.gary.movie.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tw.com.softleader.gary.movie.model.MovieModel;
import tw.com.softleader.gary.movie.service.MovieService;
import tw.com.softleader.gary.movie.service.impl.MovieServiceImpl;

public class MovieEditServlet extends HttpServlet {
	
	public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		// getting movieService from ServletContext
		MovieService movieService =  (MovieService) this.getServletContext().getAttribute("movieService");
		if(movieService == null) {
			 movieService = new MovieServiceImpl();
			 this.getServletContext().setAttribute("movieService", movieService);
		}
		
		String id = request.getParameter("id");
		MovieModel model = movieService.getById(new Long(id));
		
		response.setContentType("text/html");
		
		PrintWriter out = response.getWriter();
		
		out.println("<form action='MovieUpdateServlet' method='post'>");
		out.println("ID: <input type='hidden' name='id' value='"+model.getId()+"'/> "+model.getId()+"<br/>");
		out.println("NAME: <input type='text' name='name' value='"+model.getName()+"'/> <br/>");
		out.println("LOCAL_NAME: <input type='text' name='localName' value='"+model.getLocalName()+"'/> <br/>");
		out.println("<input type='submit'/>");
		out.println("</form>");
		
		out.println("<a href='MovieListServlet'>List</a>");
		
	}
}
